﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Blog;

namespace KerriganService.DAC
{
    public static class ArticleDAC
    {


        public static List<BlogDC> GetArticlesMoto()
        {
            List<vwGetArticlesMoto> entity = new List<vwGetArticlesMoto>();
            List<BlogDC> bo = new List<BlogDC>();
            //Boolean isValid = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetArticlesMotoes.ToList();
                    bo = ArticleDAC.EntityToCopyBolist(entity);

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }


      public static List<vwGetArticle> GetArticleToDisplay()
        {

            List<vwGetArticle> entity = new List<vwGetArticle>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetArticles.ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }

        public static List<vwGetArticlesMoto> GetArticleMotoToDisplay() {

            List<vwGetArticlesMoto> entity = new List<vwGetArticlesMoto>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetArticlesMotoes.ToList();
 
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }


        public static List<vwGetArticlesCar> GetArticleCarToDisplay()
        {

            List<vwGetArticlesCar> entity = new List<vwGetArticlesCar>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetArticlesCars.ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }


        public static List<vwGetArticlesRoadSafety> GetArticleRoadSafetyToDisplay()
        {

            List<vwGetArticlesRoadSafety> entity = new List<vwGetArticlesRoadSafety>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetArticlesRoadSafeties.ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;

        }




        public static BlogDC GetAritcleById(int id)
        {
            TBlogArticle entity = new TBlogArticle();
            BlogDC bo = new BlogDC();

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TBlogArticles.FirstOrDefault(f => f.IdBlogArticle == id);
                    bo = ArticleDAC.CopyEntityToBo(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static List<vwGetCategory> GetCategoryToDisplay() {

            List<vwGetCategory> entity = new List<vwGetCategory>();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.vwGetCategories.ToList();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            return entity;
        
        }


        public static void UpdateArticle(int id,BlogDC bo) {

            TBlogArticle entity = new TBlogArticle();
           

            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TBlogArticles.FirstOrDefault(f => f.IdBlogArticle == id);
                    ArticleDAC.UpdateEntityFromBo(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
  
        }


        public static void AddArticle(BlogDC bo)
        {

            TBlogArticle entity = new TBlogArticle();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity.Title = bo.Title;
                    entity.WrittenDate = DateTime.Now;
                    if (bo.PostedBy.IdDirectionEmployee == 0) { 
                        entity.PostedBy = (from TDirectionEmployee t in db.TDirectionEmployees where t.TPerson.IdPerson == bo.Person.IdPerson select t.TPerson.IdPerson).FirstOrDefault(); 
                    }
                    entity.IdBlogCategoryFK = bo.BlogCategory.IdBlogCategory;
                    entity.ContentTopic = bo.ContentTopic;
                    db.TBlogArticles.AddObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }



        public static void DeleteArticle(int id)
        {

            TBlogArticle entity = new TBlogArticle();


            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = db.TBlogArticles.FirstOrDefault(f => f.IdBlogArticle == id);
                    db.TBlogArticles.DeleteObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }


        private static TBlogArticle UpdateEntityFromBo(BlogDC bo,TBlogArticle entity )
        {


            try
            {
                entity.Title = bo.Title;
                entity.ContentTopic = bo.ContentTopic;
                entity.IdBlogCategoryFK = bo.BlogCategory.IdBlogCategory;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return entity;
        }


        private static BlogDC CopyEntityToBo(TBlogArticle entity)
        {
            BlogDC bo = new BlogDC();

            try
            {
                bo.Id = entity.IdBlogArticle;
                bo.BlogCategory = new BlogCategoryDC();
                bo.BlogCategory.IdBlogCategory = entity.TBlogCategory.IdBlogCategory;
                bo.BlogCategory.NameCategory = entity.TBlogCategory.Name;
                bo.Title = entity.Title;
                bo.ContentTopic = entity.ContentTopic;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }


        private static List<BlogDC> EntityToCopyBolist(List<vwGetArticlesMoto> entity)
        {
            List<BlogDC> boList = new List<BlogDC>();

            try
            {
                foreach (vwGetArticlesMoto entitys in entity)
                {
                    BlogDC bo = ArticleDAC.EntityToCopyBo(entitys);
                    boList.Add(bo);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return boList;
        }





        private static BlogDC EntityToCopyBo(vwGetArticlesMoto entity)
        {
            BlogDC bo = new BlogDC();

            try
            {
                bo.NickName = entity.NickName;
                bo.Title = entity.Title;
                bo.ContentTopic = entity.ContentTopic;
                bo.WrittenDate = entity.Date;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }
    }
}